package com.wei.duowan.bbs.ui.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.WebViewBean;
import com.wei.duowan.bbs.ui.activity.BaseActivity;
import com.wei.duowan.bbs.utils.GlideApp;
import com.wei.duowan.bbs.widget.TextClickableSpan;

import java.util.ArrayList;
import java.util.List;

import static android.text.TextUtils.isEmpty;

/**
 * 作者：赵若位
 * 时间：2019/8/30 9:54
 * 联系方式：1070138445
 * 功能：
 */
public class NewsContentAdapter extends DelegateAdapter.Adapter<BaseViewHolder>
{
    /*文本*/
    private static final int TYPE_TEXT = 1001;
    /*图片*/
    private static final int TYPE_IMG = 1002;
    /*视频*/
    private static final int TYPE_MEDIA = 1003;


    private AppCompatActivity mActivity;
    private List<String> mList;


    public NewsContentAdapter(@Nullable AppCompatActivity activity)
    {
        if (activity == null)
        {
            throw new NullPointerException("The AppCompatActivity is empty!");
        }
        this.mActivity = activity;
        this.mList = new ArrayList<>();
    }


    /*更新内容中所有的数据*/
    public void setNewData(@Nullable List<String> list)
    {
        this.mList.clear();
        if (list != null && list.size() != 0)
        {
            this.mList.addAll(list);
        }
        notifyDataSetChanged();
    }


    @Override
    public LayoutHelper onCreateLayoutHelper()
    {
        return new LinearLayoutHelper();
    }

    @Override
    public int getItemViewType(int position)
    {
        if (mList != null && mList.size() != 0 && (!isEmpty(mList.get(position))) &&
                mList.get(position).contains("[img]") && mList.get(position).contains("[/img]"))
        {
            return TYPE_IMG;
        } else if (mList != null && mList.size() != 0 && (!isEmpty(mList.get(position))) &&
                mList.get(position).contains("[media=") && mList.get(position).contains("[/media]"))
        {
            return TYPE_MEDIA;
        } else
        {
            return TYPE_TEXT;
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parnet, int type)
    {
        View layout = null;
        switch (type)
        {
            case TYPE_IMG:
                layout = LayoutInflater.from(mActivity.getApplicationContext()).inflate(R.layout.layout_news_img, parnet, false);
                break;
            case TYPE_MEDIA:
                layout = LayoutInflater.from(mActivity.getApplicationContext()).inflate(R.layout.layout_news_media, parnet, false);
                break;
            case TYPE_TEXT:
                layout = LayoutInflater.from(mActivity.getApplicationContext()).inflate(R.layout.layout_news_text, parnet, false);
                break;
        }
        return BaseViewHolder.getViewHolder(mActivity, layout);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position)
    {
        int type = getItemViewType(position);
        String data = mList.get(position);
        switch (type)
        {
            case TYPE_IMG:
                ImageView image = holder.getView(R.id.img_article);
                GlideApp.with(mActivity)
                        .asBitmap()
                        .placeholder(R.drawable.shape_drawable_error)
                        .error(R.drawable.shape_drawable_error)
                        .load(data.replaceAll("\\[img]|\\[/img]", ""))
                        .fitCenter()
                        .into(image);
                break;
            case TYPE_MEDIA:
                String media = data.replaceAll("\\[p]", "").replaceAll("\\[/p]", "");
                final String video = media.substring(media.indexOf("]") + 1, media.lastIndexOf("["));
                String cover = media.substring(media.indexOf("[media=") + 7, media.indexOf("]"));
                Log.e("media", "video:" + video);
                Log.e("media", "cover: " + cover);
                ImageView img_media = holder.getView(R.id.img_media);
                GlideApp.with(mActivity)
                        .asBitmap()
                        .placeholder(R.drawable.shape_drawable_error)
                        .error(R.drawable.shape_drawable_error)
                        .load(cover)
                        .centerCrop()
                        .into(img_media);
                holder.itemView.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        ((BaseActivity) mActivity).alert(video);
                    }
                });
                break;
            case TYPE_TEXT:
                String content = data.replaceAll("\\[p]", "").replaceAll("\\[/p]", "\n");
                if (content.contains("[url=") && content.contains("[/url]"))
                {
                    String str = content.substring(content.indexOf("]") + 1, content.lastIndexOf("["));
                    final String click = content.substring(content.indexOf("[url=") + 5, content.indexOf("]"));
                    SpannableString span = new SpannableString(str);
                    WebViewBean webViewBean = new WebViewBean(click);

                    TextClickableSpan clickSpan = new TextClickableSpan(webViewBean);
                    span.setSpan(clickSpan, 0, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    TextView article = holder.getView(R.id.tv_article);
                    article.setTextSize(TypedValue.COMPLEX_UNIT_PX, App.getResource().getDimension(R.dimen.sp_14));//设置字体大小
                    article.setText(span);
                    article.setMovementMethod(LinkMovementMethod.getInstance());
                    return;
                }
                holder.setText(R.id.tv_article, content);
                break;
            default:
                break;
        }
    }

    @Override
    public int getItemCount()
    {
        return mList == null ? 0 : mList.size();
    }
}
